package phapp.action.system;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import net.sf.json.JSONObject;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.jquery.JqueryPageHelper;
import javacommon.jquery.ListRange;
import javacommon.util.StringTool;

import phapp.model.system.Version;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;
import static javacommon.util.Struts2JsonHelper.*;

import javacommon.base.*;

import phapp.model.system.*;
import phapp.dao.system.*;
import phapp.service.system.impl.*;
import phapp.service.system.*;

/**
 * Title: ts_version_info Description: 接收前面界面请求,通过json数据格式交互 Action Description:
 * 页面通过Ajax调用Action层,返回JSONO数据 Copyright: Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

public class VersionAction extends BaseStruts2Action
{

   private VersionManager versionManager;

   /** 通过spring自动注入 */
   public void setVersionManager(VersionManager manager)
   {
      this.versionManager = manager;
   }

   /**
    * jqGrid使用列表 分页查询
    * 
    * @throws IOException
    * @author
    */
   public void list() throws IOException
   {
      PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
      Page page = versionManager.findByPageRequest(pr);

      List<Version> Versionlist = (List) page.getResult();
      ListRange<Version> resultList = new ListRange<Version>();
      resultList.setDataRows(Versionlist);
      resultList.setRecords(page.getTotalCount());
      resultList.setRows(page.getPageSize());
      if (page.getTotalCount() % page.getPageSize() > 0)
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize() + 1);
      }
      else
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize());
      }
      resultList.setPage(pr.getPageNumber());
      outJson(resultList);
   }

   /**
    * Version添加
    * 
    * @author
    * @throws IOException
    */
   public void save() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String versionJson = request.getParameter("versionJson");
         if (null != versionJson && !"".equals(versionJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(versionJson);
            Version versionobj = (Version) JSONObject.toBean(jsonobject, Version.class);
            versionManager.save(versionobj);
            result.put("success", true);
            result.put("msg", "添加成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "添加失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "添加失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   /**
    * Version修改
    * 
    * @throws IOException
    * @author
    */
   public void update() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String versionJson = request.getParameter("versionJson");
         if (null != versionJson && !"".equals(versionJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(versionJson);
            Version versionobj = (Version) JSONObject.toBean(jsonobject, Version.class);
            
            /** 设置主键值 */
            //versionobj.setId(jsonobject.get("id").toString());
            versionobj.setId(new Integer(jsonobject.get("id").toString()));
            
            versionManager.update(versionobj);
            result.put("success", true);
            result.put("msg", "修改成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "修改失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "修改失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   /**
    * Version删除
    * 
    * @throws IOException
    * @author
    */
   public void delete() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String ids = request.getParameter("ids");
         if (null != ids && !"".equals(ids))
         {
            Version version = new Version();
            version.setIdList(StringTool.stringToArray(ids));
            versionManager.removeByIdArry(version);
            result.put("success", true);
            result.put("msg", "删除成功");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "删除失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "删除失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

}
